package com.ruoyi.datastatistics.service.impl;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.datastatistics.domain.ElectricityMeterHistoryData;
import com.ruoyi.datastatistics.domain.ElectricityRealtimegatherdataXb;
import com.ruoyi.datastatistics.domain.MeterHistoryData;
import com.ruoyi.datastatistics.domain.RealtimegatherdataXb;
import com.ruoyi.datastatistics.mapper.ElectricityMeterHistoryDataMapper;
import com.ruoyi.datastatistics.mapper.MeterHistoryDataMapper;
import com.ruoyi.datastatistics.service.IElectricityMeterHistoryDataService;
import com.ruoyi.datastatistics.service.IMeterHistoryDataService;
import com.ruoyi.datastatistics.service.IRealtimegatherdataXbService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 小-历史数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-06
 */
@Service
public class MeterHistoryDataServiceImpl extends ServiceImpl
        <MeterHistoryDataMapper, MeterHistoryData> implements IMeterHistoryDataService
{
    @Autowired
    private MeterHistoryDataMapper meterHistoryDataMapper;


    @Autowired
    private IRealtimegatherdataXbService realtimegatherdataXbService;


    /**
     * 查询小-历史数据
     *
     * @param ID 小-历史数据主键
     * @return 小-历史数据
     */
    @Override
    public MeterHistoryData selectMeterHistoryDataByID(Long ID)
    {
        return meterHistoryDataMapper.selectMeterHistoryDataByID(ID);
    }

    /**
     * 查询小-历史数据列表
     *
     * @param meterHistoryData 小-历史数据
     * @return 小-历史数据
     */
    @Override
    public List<MeterHistoryData> selectMeterHistoryDataList(MeterHistoryData meterHistoryData) throws ParseException {
        if (meterHistoryData.getParams().get("beginReadDate") == null || meterHistoryData.getParams().get("endReadDate") == null){
            SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            meterHistoryData.getParams().put("beginReadDate", DateUtil.past7Days());
            meterHistoryData.getParams().put("endReadDate",foramt.format(new Date()));
        }
        return meterHistoryDataMapper.selectMeterHistoryDataList(meterHistoryData);
    }
    /**
     * 查询小-历史数据列表
     *
     * @param meterHistoryData 小-历史数据
     * @return 小-历史数据
     */
    @Override
    public List<MeterHistoryData> selectMeterHistoryDataList2(MeterHistoryData meterHistoryData) throws ParseException {
        if (meterHistoryData.getParams().get("beginReadDate") == null || meterHistoryData.getParams().get("endReadDate") == null){
            SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            meterHistoryData.getParams().put("beginReadDate", DateUtil.past7Days());
            meterHistoryData.getParams().put("endReadDate",foramt.format(new Date()));
        }
        return meterHistoryDataMapper.selectMeterHistoryDataList(meterHistoryData);
    }
    /**
     * 新增小-历史数据
     *
     * @param meterHistoryData 小-历史数据
     * @return 结果
     */
    @Override
    public int insertMeterHistoryData(MeterHistoryData meterHistoryData)
    {
        return meterHistoryDataMapper.insertMeterHistoryData(meterHistoryData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @InterceptorIgnore(tenantLine = "true")
    public AjaxResult insertMeterHistoryDataAndRealData(MeterHistoryData meterHistoryData) {
        // LambdaQueryWrapper<MeterHistoryData> queryWrapper = new LambdaQueryWrapper<>();
        // queryWrapper.eq(MeterHistoryData::getLocationNb,meterHistoryData.getLocationNb());
        // queryWrapper.eq(MeterHistoryData::getTenantId,meterHistoryData.getTenantId());
        // queryWrapper.lt(MeterHistoryData::getReadDate,meterHistoryData.getReadDate());
        // queryWrapper.ge(MeterHistoryData::getTotalFlow
        //         ,meterHistoryData.getTotalFlow());
        List<MeterHistoryData> list = meterHistoryDataMapper.selectListExtend(meterHistoryData);
        if (list.size() > 0){
            return AjaxResult.error("录入数据比上期读数要大！");
        }

        RealtimegatherdataXb realtimegatherdataXb = new RealtimegatherdataXb();
        BeanUtils.copyProperties(meterHistoryData,realtimegatherdataXb);
        realtimegatherdataXb.setReadDate(DateUtils.parseDate(meterHistoryData.getReadDate()));
        if(ObjectUtils.isNotEmpty(meterHistoryData.getValveState())){
            realtimegatherdataXb.setValveState(new BigDecimal(meterHistoryData.getValveState()));
        }
        int count =  meterHistoryDataMapper.insert(meterHistoryData);
        if (count < 1){
            return AjaxResult.error("历史表插入失败");
        }

        //实时表存在即更新  不存在则新增
        Long countRealExist = realtimegatherdataXbService.selectListCountByParams(realtimegatherdataXb);
        if(countRealExist == 1L){
            LambdaUpdateWrapper<RealtimegatherdataXb> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(RealtimegatherdataXb::getLocationNb ,realtimegatherdataXb.getLocationNb());

            boolean resultReal = realtimegatherdataXbService.update(realtimegatherdataXb, updateWrapper);
            if(!resultReal){
                throw new RuntimeException("更新失败!");
            }
        }else if(countRealExist == 0L){
            int countReal = realtimegatherdataXbService.insertRealtimegatherdataXb(realtimegatherdataXb);
            if(countReal <1){
                throw new RuntimeException("插入失败!");
            }
        }else{
            throw new RuntimeException("实时表一个location_nb存在多条重复数据!");
        }

        return AjaxResult.success();
    }

    @Override
    public int batchinsertMeterHistoryData(List<MeterHistoryData> list)
    {
        return meterHistoryDataMapper.batchinsertMeterHistoryData(list);
    }

    @Override
    public int batchinsertMeterHistoryDataR(List<MeterHistoryData> list)
    {
        return meterHistoryDataMapper.insertAllBatch(list);
        // return 1;
    }


    /**
     * 修改小-历史数据
     *
     * @param meterHistoryData 小-历史数据
     * @return 结果
     */
    @Override
    public int updateMeterHistoryData(MeterHistoryData meterHistoryData)
    {
        return meterHistoryDataMapper.updateMeterHistoryData(meterHistoryData);
    }

    /**
     * 批量删除小-历史数据
     *
     * @param IDs 需要删除的小-历史数据主键
     * @return 结果
     */
    @Override
    public int deleteMeterHistoryDataByIDs(Long[] IDs)
    {
        return meterHistoryDataMapper.deleteMeterHistoryDataByIDs(IDs);
    }

    /**
     * 删除小-历史数据信息
     *
     * @param ID 小-历史数据主键
     * @return 结果
     */
    @Override
    public int deleteMeterHistoryDataByID(Long ID)
    {
        return meterHistoryDataMapper.deleteMeterHistoryDataByID(ID);
    }

    @Override
    public List<MeterHistoryData> selectList(MeterHistoryData meterHistoryData) {
        return meterHistoryDataMapper.selectList(meterHistoryData);
    }


}
